package com.shopping.controller;

import com.shopping.constant.ResponseCode;
import com.shopping.pojo.ShoppingCart;
import com.shopping.service.shoppingCat.ShoppingCatService;
import com.shopping.util.BeanValidator;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class ShoppingCatController {

    @Autowired
    ShoppingCatService shoppingCatService;

    @PostMapping({"/shoppingCart/addShoppingCat"})
    Map<String,Object> insert(ShoppingCart shoppingCart, HttpSession httpSession){
        Map<String, Object> map = new HashMap<>();
        if(!BeanValidator.validate(shoppingCart)){
            map.put("code", ResponseCode.PARAM_ILLEGAL.getValue());
        }else{
            map = shoppingCatService.insert(shoppingCart);
        }
        return map;
    }

    @PostMapping({"/shoppingCat/deleteShoppingCat"})
    Map<String, Object> delete(@Param("id") Integer id, HttpSession httpSession){
        return shoppingCatService.delete(id);
    }

    @PostMapping({"/shoppingCat/modifyShoppingCat"})
    Map<String, Object> update(ShoppingCart shoppingCart, HttpSession httpSession){
        Map<String, Object> map = new HashMap<>();
        if(!BeanValidator.validate(shoppingCart)){
            map.put("code",ResponseCode.PARAM_ILLEGAL);
        }else{
            map = shoppingCatService.modifyShoppingCat(shoppingCart);
        }
        return map;
    }

    @PostMapping({"/shoppingCat/banchDelete"})
    Map<String, Object> banchDelete(@Param("ids") String ids){
        return shoppingCatService.batchDelete(ids);
    }

    @PostMapping({"/shoppingCat/listAll"})
    Map<String, Object> listAll(Integer userId){
        return shoppingCatService.listAll(userId);
    }
}
